﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orion.Controllers;

namespace Orion.Models
{
	public class EventRepository
	{
		OrionDBDataContext m_db = new OrionDBDataContext();

		//returns all events that have not happened yet.
		public IQueryable<Event> GetAllEvents()
		{
			var result = from e in m_db.Events
						 where e.EventDate > DateTime.Now
                         orderby e.EventDate descending
						 select e;

			return result;
		}

		public Event GetEventById(int id)
		{
			var result = (from e in m_db.Events
						  where e.ID == id
						  select e).SingleOrDefault();

			return result;
		}

		public void AddEvent(Event e)
		{
			m_db.Events.InsertOnSubmit(e);
			m_db.SubmitChanges();
		}

		public void UpdateEvent(Event e)
		{
			var temp = GetEventById(e.ID);

			if( ! string.IsNullOrEmpty( e.Picture ) )
			{
				temp.Picture = e.Picture;
			}
			temp.Address = e.Address;
			temp.EventDate = e.EventDate;
			temp.PostCode = e.PostCode;
			temp.TextBody = e.TextBody;
			temp.Title = e.Title;
			temp.UserGroup = e.UserGroup;

			m_db.SubmitChanges();
		}
	}
}